package cn.itcast.dagger_demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import javax.inject.Inject;

import cn.itcast.dagger_demo.bean.Data;
import cn.itcast.dagger_demo.bean.TestData;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";
    private TextView mTextViewContent;

    @Inject
    DaggerPresenter daggerPresenter;
    @Inject
    OkHttpClient okHttpClient;
    @Inject
    Retrofit retrofit;

    private Callback<Data> testDataCallback = new Callback<Data>() {
        @Override
        public void onResponse(Call<Data> call, Response<Data> response) {

            Log.d(TAG, "onResponse() called" + Thread.currentThread().getName());
            if (call instanceof TestData) {


            }

            Data testData = response.body();

            daggerPresenter.showData(testData.toString());
        }

        @Override
        public void onFailure(Call<Data> call, Throwable t) {

            Log.e(TAG, "t:" + t.getMessage());
            Toast.makeText(MainActivity.this, "t:" + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    };;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextViewContent = ((TextView) findViewById(R.id.textview_content));

        AppComponent appComponent = ((MyApplication) getApplication()).getAppComponent();
        DaggerActivityComponent.builder().appComponent(appComponent).activityModule(new ActivityModule(this)).build().inject(this);



     /*   try {
            Response<TestData> execute = testData.execute();
            TestData body = execute.body();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }

    public void requestData(View view) {

        TestResponse testResponse = retrofit.create(TestResponse.class);
        Call<Data> testData = testResponse.getTestData();
        testData.clone().enqueue(testDataCallback);
    }

    public void getCartData(View view) {

        TestResponse testResponse = retrofit.create(TestResponse.class);
        Call<Data> cartData = testResponse.getCartData("1:3:1,2,3,4|2:2:2,3");
        cartData.clone().enqueue(testDataCallback);
    }

    public <T> void setData(T data) {

        mTextViewContent.setText(data.toString());
    }
}
